package com.zhou.common.lib.util;

import com.google.common.collect.Maps;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 18576666986
 * @description
 * @createDate 2023/4/13 19:07
 */
public class HttpUtil {


//    private static final Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");

    private static final Pattern hostPattern = Pattern.compile("(?<=(^https://|^http://))([\\w.-]+)");
    private static final Pattern basePathP = Pattern.compile("^(https://|http://)([\\w.:-]+)");
    private static final Pattern fileNameP = Pattern.compile("([\\w.]+)(?=#|$)");


    public static Map<String, String> getHeaders(HttpServletRequest request) {
        Map<String, String> map = Maps.newHashMap();
        Enumeration names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement().toString();
            String value = request.getHeader(name);
            map.put(name, value);
        }
        return map;
    }

    public static RequestCopy getRequest(HttpServletRequest request) {
        RequestCopy requestCopy = new RequestCopy();
        if (request != null) {
            Map<String, String> headers = getHeaders(request);
            requestCopy.setHeaders(headers);
            requestCopy.setRequestUri(request.getRequestURI());
        }
        return requestCopy;
    }

    public static String getFullUlr(String basePath, String partPath) {
        if (partPath.contains("http")) {
            return partPath;
        } else if (partPath.startsWith("//")) {
            return basePath.split("//")[0] + partPath;
        } else if (partPath.startsWith("/")) {
            Matcher m = basePathP.matcher(basePath);
            if (m.find()) {
                basePath = m.group();
            }
            return basePath.endsWith("/") ? basePath.substring(0, basePath.length() - 1) + partPath : basePath + partPath;
        } else if (partPath.startsWith("./")) {
            partPath = partPath.substring(1, partPath.length());
            return basePath.endsWith("/") ? basePath.substring(0, basePath.length() - 1) + partPath : basePath + partPath;
        }
        return basePath.endsWith("/") ? basePath + partPath : basePath + "/" + partPath;
    }

    public static String getFileName(String path) {
        String returnVal = StringUtils.EMPTY;
        try {
            Matcher m = fileNameP.matcher(path);
            if (m.find()) {
                returnVal = m.group();
            }

        } catch (Exception e) {
        }
        return returnVal;
    }

    public static String getBasePath(String url) {
        if (!(StringUtils.startsWithIgnoreCase(url, "http://") || StringUtils
                .startsWithIgnoreCase(url, "https://"))) {
            url = "http://" + url;
        }
//    Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");

        String returnVal = StringUtils.EMPTY;
        try {
            Matcher m = basePathP.matcher(url);
            if (m.find()) {
                returnVal = m.group();
            }

        } catch (Exception e) {
        }
        if ((StringUtils.endsWithIgnoreCase(returnVal, ".html") || StringUtils
                .endsWithIgnoreCase(returnVal, ".htm"))) {
            returnVal = StringUtils.EMPTY;
        }
        return returnVal;
    }

    public static String getHost(String url) {
        if (!(StringUtils.startsWithIgnoreCase(url, "http://") || StringUtils
                .startsWithIgnoreCase(url, "https://"))) {
            url = "http://" + url;
        }
        String returnVal = StringUtils.EMPTY;
        try {
            Matcher m = hostPattern.matcher(url);
            if (m.find()) {
                returnVal = m.group();
            }

        } catch (Exception e) {
        }
        if ((StringUtils.endsWithIgnoreCase(returnVal, ".html") || StringUtils
                .endsWithIgnoreCase(returnVal, ".htm"))) {
            returnVal = StringUtils.EMPTY;
        }
        return returnVal;
    }

    public static void main(String[] args) {
        System.out.println(getHost("https://qinqing.zhengzhou.gov.cn:4431/qqzxuc-test/"));
    }


}
